// pages/bookEnter/ranking/second/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _id: "",
    data: "",
    allDataBooks: "",
    dataBooks: "",

    opacity: 0,
    flog: 1,
    ADDNUM: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    this.save_id(options._id) // 保存榜单传递过来的id值

    this.getData() // 根据传递来的id获取数据
  },

  save_id(_id) {
    this.setData({
      _id
    })
  },

  getData() {
    let id = this.data._id;
    let that = this;
    wx.request({
      url: `https://api.zhuishushenqi.com/ranking/${id}`,
      success: res => {
        let data = res.data.ranking;
        data.books.forEach(val => {
          let src = val.cover.indexOf("http");
          src = decodeURIComponent(val.cover.substring(src)); // 解码状态
          val.cover = src;
        }) // 解码图片的地址并且获取到正确数据

        if (data.gender === "male") {
          data.gender = "男生专区"
        } else if (data.gender === "female") {
          data.gender = "女生专区"
        } else if (data.gender === "picture") {
          data.gender = "出版专区"
        } else if (data.gender === "epub") {
          data.gender = "电子书专区"
        } // 更改来源英文显示切换中文显示        

        let num = data.updated.indexOf("T");
        data.updated = data.updated.slice(0, num);
        // 更改时间显示

        console.log(res)
        that.setData({
          data: res.data.ranking
        })
        that.setData({
          allDataBooks: res.data.ranking.books
        })
        that.setData({
          dataBooks: res.data.ranking.books.slice(0, that.data.ADDNUM)
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

    let dataLength = this.data.dataBooks.length;
    console.log(dataLength)
    const ADDNUM = this.data.ADDNUM;

    if (dataLength < ADDNUM * this.data.flog) {
      this.setData({
        opacity: 1
      })
      let that = this;
      setTimeout(function() {
        that.setData({
          opacity: 0
        });
      }, 3000)
      return
    }

    this.data.flog++;
    let dataAllLength = this.data.allDataBooks.length;
    if (dataAllLength > dataLength) {
      this.setData({
        dataBooks: this.data.allDataBooks.slice(0, ADDNUM * this.data.flog)
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },


  pushBookIntro(e) {
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: `/pages/bookIntro/intro-start/index?id=${id}`
    })
  }
})